#include"progressbar.h"
#include<stdlib.h>
#include<string.h>
#define SIZE 101
#define STYLE '='
//version1
//void processbar()
//{
//  char buffer[SIZE];
//  memset(buffer,0,sizeof(buffer));
//  const char* ch = "|/-\\";
//  int cnt = 0;
//  int len = strlen(ch);
//  while(cnt <= 100)
//  {
//    printf("[%-100s] [%d%%] [%c]\r",buffer,cnt,ch[cnt%len]);
//    fflush(stdout);
//    buffer[cnt] = STYPE;
//    cnt++;
//    usleep(30000);
//  }
//  printf("\n");
//
//}
//
//

//version2
void FlushProcess(double total, double curr)//
{
    if(curr > total)
        curr = total;

    double rate = curr / total * 100; // 1024.0 , 512.0 -> 0.5 -> 50.0
    int cnt = (int)rate; // 50.8 , 49.9 -> 50, 49
    char processbuff[SIZE];
    memset(processbuff, '\0', sizeof(processbuff));
    int i = 0;
    for(; i < cnt; i++)
        processbuff[i] = STYLE;
    
    //旋转光标不应该跟total和curr有关系，应该跟FlushProcess的调用次数有关
    //网速太慢，不足以推进进度，但是函数依旧在被调用，旋转光标依旧在动
    static const char *lable = "|/-\\";//属于函数
    static int index = 0;
    // 刷新
    printf("[%-100s][%.1lf%%][%c]\r", processbuff, rate, lable[index++]);
    index %= strlen(lable);
    fflush(stdout);
    if(curr >= total)
    {
        printf("\n");
    }
  }

